//
//  UIBarbutton+Extension.swift
//  Weibo
//
//  Created by 任贤斌 on 2016/11/21.
//  Copyright © 2016年 Renkey. All rights reserved.
//

import UIKit

//给UIBarbuttonItem扩展构造方法
extension UIBarButtonItem {
    
    //分类中不能声明指定的构造器
    convenience init(title: String = "", imageName: String, target: Any?, action: Selector?) {
        //设置导航栏右边的item
        let btn = UIButton()
        btn.setTitle(title, for: .normal)
        //设置文字颜色
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        //设置文字大小
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //设置按钮
        btn.setImage(UIImage(named: imageName), for: .normal)
        btn.setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
        //监听按钮的点击事件
        if let ac = action {
            btn.addTarget(target, action: ac, for: .touchUpInside)
        }
        
        btn.sizeToFit()
        
        self.init()
        
        self.customView = btn
    }
}
